package com.fl.iocaop.java8;

import com.fl.iocaop.domain.Dept;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class stream {
    private static List<Dept> deptList=new ArrayList<>();

    private static Map<String,List<apple>> map = new HashMap<>();

    private static List<apple> apples=new ArrayList<>();
    static {
        deptList.add(new Dept("腾讯","1"));
        deptList.add(new Dept("腾讯2","1"));
        deptList.add(new Dept("腾讯2","1"));
        deptList.add(new Dept("汪东城","2"));
        deptList.add(new Dept("六得到的","3"));
    }
    static {
        List<apple> list = new ArrayList<>();
        apples.add(new apple("黄色",50, apple.status.FREE));
        apples.add(new apple("绿色",60,apple.status.FREE));
        apples.add(new apple("嘿色",60,apple.status.SUCCESSF));
        apples.add(new apple("红色",85,apple.status.SUCCESSF));
        apples.add(new apple("白色",100,apple.status.FULL));
//        map.put("黄色",50)
    }
    //java8特性stream流与lanmbda表达式的使用
    public void  test(){
        List<Dept> depts = deptList.stream()
                .filter(a -> a.getDeptId().equals("1"))
                .filter(b->b.getDeptName().equals("腾讯2"))
                .collect(Collectors.toList());
        System.out.println(depts.toString());
    }
    //利于java8新特性求出apples集合的每种颜色苹果的平均值
    private void test2(){
        apples.stream().collect(Collectors.groupingBy(a->a.getColor(),
                Collectors.averagingInt(b->b.getWeight())))
                .forEach((k,v)-> System.out.println(k+":"+v));
    }

    public static void main(String[] args) {
        new stream().test2();
    }
}
